#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define XH(i,a,b) for(int i = a; i < b; ++i)
#define XXH(i,b,a) for(int i = b; i >= a; --i)
#define Module 998244353
int tmpInt;LL tmpLL;string tmpStr;char tmpChar;
vector<int>tmpIntVector;

LL fun(string s, int size, LL base, LL sample){
  LL num = 0;
  XH(i,0,size){
    sample -= (LL)(s[size - 1 -i] - '0');
    if(sample < 0)return 1;
    sample = sample/base;
  }
  return -1;
}

int main(){
  string X;LL M;cin>>X>>M;
  if(X.size() == 1){
    LL num = X[0] - '0';
    if(num <= M)cout<<1<<endl;
    else cout<<0<<endl;
    return 0;
  }
  char da = '0';
  int size_x = X.size();
  XH(i,0,size_x){
    if(X[i]>da)da = X[i];
  }
  LL left = 1;
  LL right = LLONG_MAX;
  XH(i,0,100){
    LL mid = left + (right - left)/2;
    if(fun(X,size_x,mid,M) == 1) right = mid;
    else left = mid;
  }
  LL ans = max(0ll,left-(da-'0'));
  cout<<ans<<endl;

  return 0;
}
